package com.naizhuai.ui.ainote.note;

import android.support.annotation.NonNull;

import com.naizhuai.Model.ModelProvider;
import com.naizhuai.Model.db.ainote.Note;
import com.naizhuai.Model.db.ainote.NoteDataSourse;
import com.naizhuai.Model.db.ainote.NoteRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：lichenxi
 * @date 2018/2/22 14
 * email：525603977@qq.com
 * Fighting
 */
public class NotePresenter implements NoteContract.Presenter {
    private NoteDataSourse mRepository;
    private Note.NoteFilterType mCurrentFilterType= Note.NoteFilterType.LAST_UPDATE;
    private NoteContract.View mView;
    public NotePresenter() {
        mRepository = ModelProvider.provideNotesRepository();
    }

    @Override
    public void takeView(NoteContract.View view) {
       this.mView=view;
       loadNotes();
    }

    @Override
    public void loadNotes() {
        mView.setLoadingIndicator();
        switch (mCurrentFilterType){
            case TITLE:
                loadTitleFilterNote();
                break;
            case CREATION:
                loadCreationFilterNote();
                break;
            case LAST_UPDATE:
                loadLastUpdateFilterNote();
                break;
            default:
                loadLastUpdateFilterNote();
        }
    }

    private void loadLastUpdateFilterNote() {
        mRepository.getNotes(new NoteDataSourse.LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                mView.showNotes(notes);
            }

            @Override
            public void onDataNotAvailable() {
                mView.showNoTasks();
            }
        });
    }

    private void loadCreationFilterNote() {
        mRepository.getCreationNotes(new NoteDataSourse.LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                mView.showNotes(notes);
            }

            @Override
            public void onDataNotAvailable() {
                mView.showNoTasks();
            }
        });
    }
    private void loadTitleFilterNote() {
        mRepository.getTitleNotes(new NoteDataSourse.LoadNotesCallback() {
            @Override
            public void onNotesLoaded(List<Note> notes) {
                mView.showNotes(notes);
            }

            @Override
            public void onDataNotAvailable() {
                mView.showNoTasks();
            }
        });
    }
    @Override
    public void dropView() {
        this.mView=null;
    }

    @Override
    public void setFiltering(Note.NoteFilterType requestType) {
        this.mCurrentFilterType=requestType;
    }

    @Override
    public void deleteNote(ArrayList notes) {
         mRepository.deleteNotes(notes);
         mView.showRecycleViewItem(notes);

    }
}
